package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public DishDto getWithFlavor(Long id) {
        //1.查询菜品基本信息
        Dish dish = getById(id);//在执行sql相关的语句 展现出来
        System.out.println("===========");
        System.out.println(dish);
        System.out.println("============");
        //2.查询菜品口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);
        System.out.println("!!!!!!!!!!!!!!!!!");
        System.out.println(dishFlavors);
        //[DishFlavor(id=1656094947916992513, dishId=1656094946730004482, name=温度, value=["热饮","常温","去冰","少冰","多冰"], createTime=2023-05-10T16:50:17, updateTime=2023-05-10T16:50:17, createUser=1, updateUser=1, isDeleted=0), DishFlavor(id=1656220098318909441, dishId=1656094946730004482, name=温度, value=["热饮","常温","去冰","少冰","多冰"], createTime=2023-05-10T16:50:17, updateTime=2023-05-10T16:50:17, createUser=1, updateUser=1, isDeleted=0)]
        System.out.println("!!!!!!!!!!!!!!!!!");

        //3.封装到Dto
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish,dto);
        dto.setFlavors(dishFlavors);

        return dto;
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dto) {
        //保存菜品的基本信息Dish
        save(dto);
        //保存菜品的口味信息到dish_flavor
        List<DishFlavor> flavors = dto.getFlavors();//---问里边有什么 手写
        //设置dishId
//        String s = flavors.toString();
//        System.out.println(s);
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dto.getId()));


        dishFlavorService.saveBatch( flavors);
    }
    @Autowired
    DishMapper dishMapper;
    @Override
    public Page<DishDto> pageByName(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page,pageSize);
        return  dishMapper.pageByName(pageInfo,name);
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dto) {
        //1.根据id修改菜品信息
        updateById(dto);
        Long dishId = dto.getId();//获取用户闯过来的菜品id
        //2.先删除这个菜品的所有口味
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishId);
        dishFlavorService.remove(lqw);
        //3.添加新口味
        List<DishFlavor> flavors = dto.getFlavors();

        //设置dishId
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));

        dishFlavorService.saveBatch(flavors);
    }
}
